package org.htmlparser.parserapplications.filterbuilder;

import java.awt.Component;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.Translate;

public class HtmlTreeCellRenderer extends DefaultTreeCellRenderer
  implements TreeCellRenderer
{
  public HtmlTreeCellRenderer()
  {
    setLeafIcon(null);
    setClosedIcon(null);
    setOpenIcon(null);
  }

  public Component getTreeCellRendererComponent(JTree paramJTree, Object paramObject, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, int paramInt, boolean paramBoolean4)
  {
    super.getTreeCellRendererComponent(paramJTree, paramObject, paramBoolean1, paramBoolean2, paramBoolean3, paramInt, paramBoolean4);
    Node localNode = (Node)paramObject;
    if (localNode instanceof TagNode)
      setText(toHtml((TagNode)localNode));
    while (true)
    {
      while (true)
      {
        return this;
        if (!(localNode instanceof TextNode))
          break;
        setText(toText((TextNode)localNode));
      }
      setText(localNode.toHtml());
    }
  }

  public String toHtml(TagNode paramTagNode)
  {
    int i2;
    int i = 0;
    int j = 2;
    Vector localVector = paramTagNode.getAttributesEx();
    int k = localVector.size();
    for (int l = 0; l < k; ++l)
      j += ((Attribute)localVector.elementAt(l)).getLength();
    StringBuffer localStringBuffer1 = new StringBuffer(j);
    localStringBuffer1.append("<");
    for (int i1 = 0; i1 < k; ++i1)
      ((Attribute)localVector.elementAt(i1)).toString(localStringBuffer1);
    localStringBuffer1.append(">");
    String str = Translate.encode(localStringBuffer1.toString());
    if (paramTagNode.getChildren() != null)
      i2 = 1;
    while (true)
    {
      int i3 = 13 + str.length();
      if (i2 != 0)
        i = 16;
      StringBuffer localStringBuffer2 = new StringBuffer(i3 + i);
      localStringBuffer2.append("<html>");
      if (i2 != 0)
        localStringBuffer2.append("<font color=\"blue\">");
      localStringBuffer2.append(str);
      if (i2 != 0)
        localStringBuffer2.append("</font>");
      localStringBuffer2.append("</html>");
      return localStringBuffer2.toString();
      i2 = 0;
    }
  }

  public String toText(TextNode paramTextNode)
  {
    int i = paramTextNode.getStartPosition();
    StringBuffer localStringBuffer = new StringBuffer(20 + paramTextNode.getEndPosition() - i);
    String str = paramTextNode.toHtml();
    int j = 0;
    while (true)
    {
      if (j < str.length())
      {
        char c = str.charAt(j);
        switch (c)
        {
        case '\11':
        case '\f':
        default:
          localStringBuffer.append(c);
        case '\t':
        case '\n':
        case '\r':
        }
      }
      while (true)
      {
        while (true)
        {
          while (true)
          {
            if (77 > localStringBuffer.length())
              break label146;
            localStringBuffer.append("...");
            return localStringBuffer.toString();
            localStringBuffer.append("\\t");
          }
          localStringBuffer.append("\\n");
        }
        localStringBuffer.append("\\r");
      }
      label146: ++j;
    }
  }
}